import * as CommonSelectors from '../lib/common_selectors';
import doubleClickZoom from '../lib/double_click_zoom';
import * as Constants from '../constants';
import PlotUtils from "../util/PlotUtils";
import DrawUtil from "../util/DrawUtils";

//弧线
const DIRECT_ARC_LINE = {};

DIRECT_ARC_LINE.onSetup = function () {
    const polygon = this.newFeature({
        type: Constants.geojsonTypes.FEATURE,
        properties: {
            type: 'arc_line',
            points: []
        },
        geometry: {
            type: Constants.geojsonTypes.LINE_STRING,
            coordinates: []
        }
    });

    this.addFeature(polygon);

    this.clearSelectedFeatures();
    doubleClickZoom.disable(this);
    this.map.dragPan.disable();
    this.updateUIClasses({mouse: Constants.cursors.ADD});
    this.activateUIButton(Constants.types.CIRCLE);
    this.setActionableState({
        trash: true
    });

    return {
        polygon,
        currentVertexPosition: 0
    };
};

DIRECT_ARC_LINE.clickAnywhere = function (state, e) {

    if (state.currentVertexPosition === 0) {
        state.currentVertexPosition++;
        return;
    }

    const points = state.polygon.properties.points;

    if (points.length > 1) {
        const lastCoordinate = points[points.length - 1];
        const isDuplicate = lastCoordinate[0] === e.lngLat.lng && lastCoordinate[1] === e.lngLat.lng
        const circleFeature = DrawUtil.drawArcLine(isDuplicate ? points :
            DrawUtil.copyPointsAndAddCoordinate(points,[e.lngLat.lng, e.lngLat.lat]))
        state.polygon.incomingCoords(circleFeature);
    }


    if (points.length > 2) {
        return this.changeMode(Constants.modes.SIMPLE_SELECT, {featureIds: [state.polygon.id]});
    }

};

DIRECT_ARC_LINE.onMouseMove = function (state, e) {
    state.polygon.updateCoordinate(`0.${state.currentVertexPosition}`, e.lngLat.lng, e.lngLat.lat);
    if (CommonSelectors.isVertex(e)) {
        this.updateUIClasses({mouse: Constants.cursors.POINTER});
    }
};

DIRECT_ARC_LINE.onTap = DIRECT_ARC_LINE.onClick = function (state, e) {
    console.log(state.polygon.coordinates[0]);
    if (CommonSelectors.isVertex(e)) return this.clickOnVertex(state, e);
    return this.clickAnywhere(state, e);
};

DIRECT_ARC_LINE.onKeyUp = function (state, e) {
    if (CommonSelectors.isEscapeKey(e)) {
        this.deleteFeature([state.polygon.id], {silent: true});
        this.changeMode(Constants.modes.SIMPLE_SELECT);
    } else if (CommonSelectors.isEnterKey(e)) {
        this.changeMode(Constants.modes.SIMPLE_SELECT, {featureIds: [state.polygon.id]});
    }


};

DIRECT_ARC_LINE.onStop = function (state) {
    this.updateUIClasses({mouse: Constants.cursors.NONE});
    doubleClickZoom.enable(this);
    this.activateUIButton();

    // check to see if we've deleted this feature
    if (this.getFeature(state.polygon.id) === undefined) return;

    //remove last added coordinate
    // state.polygon.removeCoordinate(`0.${state.currentVertexPosition}`);
    if (state.polygon.isValid()) {
        this.map.fire(Constants.events.CREATE, {
            features: [state.polygon.toGeoJSON()]
        });
    } else {
        this.deleteFeature([state.polygon.id], {silent: true});
        this.changeMode(Constants.modes.SIMPLE_SELECT, {}, {silent: true});
    }
};

DIRECT_ARC_LINE.toDisplayFeatures = function (state, geojson, display) {

    const isActivePolygon = geojson.properties.id === state.polygon.id;
    geojson.properties.active = (isActivePolygon) ? Constants.activeStates.ACTIVE : Constants.activeStates.INACTIVE;
    return display(geojson);
};

DIRECT_ARC_LINE.onTrash = function (state) {
    this.deleteFeature([state.polygon.id], {silent: true});
    this.changeMode(Constants.modes.SIMPLE_SELECT);
};


DIRECT_ARC_LINE.onMouseDown = DIRECT_ARC_LINE.onTouchStart = function (state, e) {
    const currentPoints = state.polygon.properties.points;
    if (currentPoints.length < 3) {
        state.polygon.properties.points.push([e.lngLat.lng, e.lngLat.lat]);
    }
};

DIRECT_ARC_LINE.onDrag = DIRECT_ARC_LINE.onMouseMove = function (state, e) {
    const points = state.polygon.properties.points;
    if (points.length > 1) {
        const lastCoordinate = points[points.length - 1];
        const isDuplicate = lastCoordinate[0] === e.lngLat.lng && lastCoordinate[1] === e.lngLat.lng
        const circleFeature = DrawUtil.drawArcLine(isDuplicate ? points :
            DrawUtil.copyPointsAndAddCoordinate(points,[e.lngLat.lng, e.lngLat.lat]))
        state.polygon.incomingCoords(circleFeature);
    }

};

DIRECT_ARC_LINE.onMouseUp = DIRECT_ARC_LINE.onTouchEnd = function (state, e) {
    this.map.dragPan.enable();
    return this.changeMode(Constants.modes.SIMPLE_SELECT, {featureIds: [state.polygon.id]});
};



export default DIRECT_ARC_LINE;

